package com.htfyun.ft.view.myrecyclerview;

import android.content.res.Resources;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.core.view.GestureDetectorCompat;
import androidx.recyclerview.widget.RecyclerView;

public abstract class OnRecyclerItemClickListener implements RecyclerView.OnItemTouchListener {

    private GestureDetectorCompat mGestureDetector;
    private RecyclerView recyclerView;


    public OnRecyclerItemClickListener(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new ItemTouchHelperGestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
         mGestureDetector.onTouchEvent(e);
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }

    private class ItemTouchHelperGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null) {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
                onItemClick(vh, e);
            }
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (child != null) {
                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(child);
                onItemLongClick(vh, e);
            }
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            int mini_width = Resources.getSystem().getDisplayMetrics().densityDpi;
            int mini_speed = 10;
            float distance_right = e2.getX() - e1.getX();
            float distance_left = e1.getX() - e2.getX();
            float distance_down = e2.getY() - e1.getY();
            float distance_up = e1.getY() - e2.getY();


            if (distance_up > mini_width && Math.abs(velocityX) > mini_speed) {
                onRecyclerViewFling(FlingDirection.up);
            } else if (distance_down > mini_width && Math.abs(velocityX) > mini_speed) {
                onRecyclerViewFling(FlingDirection.down);
            } else if (distance_right > mini_width && Math.abs(velocityX) > mini_speed) {
                onRecyclerViewFling(FlingDirection.right);
            } else if (distance_left > mini_width && Math.abs(velocityX) > mini_speed) {
                onRecyclerViewFling(FlingDirection.left);
            }

            return true;
        }
    }

    public abstract void onItemClick(RecyclerView.ViewHolder vh, MotionEvent e);

    public void onRecyclerViewFling(FlingDirection direction) {

    }

    public void onItemLongClick(RecyclerView.ViewHolder vh, MotionEvent e) {
    }

    public enum FlingDirection {
        left,
        right,
        up,
        down,
    }

}

